/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.product.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.product.entity.Attr;
import com.ocm.product.vo.AttrVO;
import com.ocm.product.wrapper.AttrWrapper;
import com.ocm.product.service.IAttrService;
import com.ocm.core.boot.ctrl.BladeController;

import java.util.Map;

/**
 *  控制器
 *
 * @author HQY
 * @since 2021-11-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/attr")
@Api(value = "", tags = "接口")
public class AttrController extends BladeController {

	private final IAttrService attrService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入attr")
	public R<AttrVO> detail(Attr attr) {
		Attr detail = attrService.getOne(Condition.getQueryWrapper(attr));
		return R.data(AttrWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入attr")
	public R<IPage<AttrVO>> list(Attr attr, Query query) {
		IPage<Attr> pages = attrService.page(Condition.getPage(query), Condition.getQueryWrapper(attr).orderByAsc("sort"));
		return R.data(AttrWrapper.build().pageVO(pages));
	}




	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入attr")
	public R<IPage<AttrVO>> page(@RequestParam Map<String, Object> attr, Query query) {
		IPage<AttrVO> attrPage;
		//两种查询类型 即本分类关联属性和未关联属性
		//未关联属性 使用 not in sql语句
		if(attr.containsKey("categoryId")){
			attrPage = attrService.selectAllAttrPage(Condition.getPage(query),
				attr.get("attrName")==null?null:String.valueOf(attr.get("attrName")),
				Long.valueOf(attr.get("categoryId").toString()));
			return R.data(attrPage);
		}
		//关联属性 join表后直接返回
		attrPage = attrService.selectAttrPage(Condition.getPage(query),
			Condition.getQueryWrapper(attr,AttrVO.class));
		return R.data(attrPage);
	}



	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入attr")
	public R save(@Valid @RequestBody Attr attr) {
		return R.status(attrService.save(attr));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入attr")
	public R update(@Valid @RequestBody Attr attr) {
		return R.status(attrService.updateById(attr));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入attr")
	public R submit(@Valid @RequestBody Attr attr) {

		return R.status(attrService.saveOrUpdate(attr));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(attrService.deleteLogic(Func.toLongList(ids)));
	}


}
